$(function () {
    init_track_table();
    getAllTagGroup();
})
function init_track_table() {
    var url = ctx + "saleConfig/initTrackTable";
    $.post(url, {}, function (r) {
        if (r.code === 0) {
            var data = r.data;
            for (var i = 0; i < data.length; i++) {
                var name = '';
                if (i === 1) {name = "阶段一"
                } else if (i === 2) {name = "阶段二"
                } else if (i === 3) {name = "阶段三"
                } else if (i === 4) {name = "阶段四"
                } else if (i === 0) {name = "初始"}
                $("#sale_track_tbody").append("<tr><td class='text-center'>" + name + "</td>\n" +
                    " <td class='text-center'><a href='javascript:void(0);' id='" + data[i].salePkid + "' >" + data[i].saleName + "</a></td></tr>");
            }
            init_firstTab();
        } else {
            $JuXue.n_danger("初始化阶段跟踪失败");
        }
    });
    //获取公海资源配置
    var url = ctx+"SeasConfig/getSealConfig";
    $.getJSON(url,{},function (r) {
        if (r.code == 0){
            var data = r.msg;
            for (var i = 0; i < data.length; i++) {
                var type = data[i].sgType;
                var status = data[i].sgStatus;
                var tr = "";
                if (status == 0){
                     tr = "<tr><td class='text-center'>" + data[i].sgName + "</td>\n" +
                        " <td class='text-center'><a href='#' id='" + data[i].sgPkid + "' >" + data[i].sgDay + "</a></td>" +
                        "<td><button type='button' onclick='changeUpStatus(this)' data='"+data[i].sgId+"' class='btn btn-sm btn-success'>正常</button></td></tr>";
                }else{
                    tr = "<tr><td class='text-center'>" + data[i].sgName + "</td>\n" +
                        " <td class='text-center'><a href='#' id='" + data[i].sgPkid + "' >" + data[i].sgDay + "</a></td>" +
                        "<td><button type='button' onclick='changeDownStatus(this)' data='"+data[i].sgId+"' class='btn btn-sm btn-danger'>暂停</button></td></tr>";
                }
                if(type==1){
                    $("#big_tbody").append(tr)
                }else if(type==2){
                    $("#exemp_tbody").append(tr);
                }else{
                    $("#ai_tbody").append(tr);
                }
            }
            init_secondTab();
        } else{
            $JuXue.n_danger("初始化格式失败");
        }
    });
}
/**d多选和单选**/
function multiLabel() {
    var checked =$("#multicheckBox").attr("checked");
    var uuid = $("#multicheckBox").attr("uuid");
    if (checked == "checked") {
        $("#multicheckBox").removeAttrs("checked");
    }else{$("#multicheckBox").attr("checked","checked");}
    var ischakex = "1";
    if (checked == "checked"){ischakex = "0";}
    $.post(ctx+"saleConfig/updateGroupName",{tagUuid:uuid,tagSelect:ischakex},function (r) {
        if (r.code == 0){$JuXue.n_success("修改成功");
        }else{$JuXue.n_warning("修改失败");}
    });
}
/*加载标签分组列表*/
function getAllTagGroup() {
    var url = ctx + "saleConfig/getAllTagGroup";
    $.post(url, {}, function (r) {
        if (r.code === 0) {
            var data = r.data.group;
            if(data.length>0){
                /*加载标签分组列表*/
                var name = data[0].tagGroupName;
                var uuid = data[0].tagUuid;
                $("#tagGroupTitle").text(name);$("#sale-tag-selectid").val(uuid);
                var flag = true;
                $("#sale-tagGroup-list").empty();
                $.each(data, function (index, val) {
                    var tag = "<div class='fc-event' onclick='selectTagName(this)' uuid='" + val.tagUuid + "' tageName='" + val.tagGroupName + "'>" +
                        "<i class='fa fa-paperclip' ></i><a href='javascript:;'>" + val.tagGroupName + "</a>" +
                        "<a href='javascript:;' uuid='"+val.tagUuid+"' onclick='loadDelTagGroup(this)'><i class='fa fa-trash-o pull-right'></i></a>" +
                        "<a href='javascript:;' uuid='"+val.tagUuid+"' name='"+val.tagGroupName+"' onclick='loadUpdateGroup(this)'><i class='fa fa-edit pull-right'></i></a>" +
                        "<div>";
                    $("#sale-tagGroup-list").append(tag);
                    if(flag && val.tagSelect == "1"){
                        $("#multicheckBox").attr("checked","checked");
                        $("#multiLabel").addClass("active");
                        $("#multicheckBox").attr("uuid",val.tagUuid);
                    }
                    flag = false;
                });
                /*加载第一个标签分组下的标签*/
                var tags = r.data.tag;
                $("#sale-tag-lists").empty();
                $.each(tags,function(index,val){
                    var button = "<li class='p-r-10 dropdown pull-left m-b-5' >" +
                        "<a class='p-t-3 p-r-10 p-b-3 p-l-10 dropdown-toggle btn' data-toggle='dropdown' style='border: 1px darkgrey solid'>"+val.tagName+"  <i class='fa fa-angle-down'></i></a>" +
                        "<ul class='dropdown-menu media-list pull-left animated fadeInDown'>" +
                        "<a href='javascript:;' uuid='"+val.tagUuid+"' name='"+val.tagName+"' onclick='updateTagName(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-pencil-square-o p-r-10'></i>修改名称</h5></div></div></li></a>" +
                        "<a href='javascript:;' uuid='"+val.tagUuid+"' onclick='deleteTag(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-trash-o p-r-15'></i>删除</h5></div></div></li></a>" +
                        "</ul></li>";
                    $("#sale-tag-lists").append(button);
                });
            }else{
                $("#sale-tagGroup-list").empty();
                $("#tagGroupTitle").text("还未创建分组");
            }
        }
    });
}

/*加载创建标签分组页面*/
function loadCreateGrouping() {
    var url = ctx + "saleConfig/loadCreateGrouping";
    $.post(url, {}, function (r) {
        $("#sale-modal").empty().append(r).modal("show").draggable();
    })
}

/*点击切换标签组*/
function selectTagName(btn) {
    var tagName = $(btn).attr("tageName");
    var uuid = $(btn).attr("uuid");
    $("#tagGroupTitle").text(tagName);
    $("#sale-tag-selectid").val(uuid);
    $("#sale-tag-lists").empty();
    $.post(ctx+"saleConfig/chooseGroup",{"uuid":uuid},function (r) {
       var data = r.data.list;
        if(data.length>0){
            $.each(data,function(index,val) {
                var button = "<li class='p-r-10 dropdown pull-left m-b-5' >" +
                    "<a class='p-t-3 p-r-10 p-b-3 p-l-10 dropdown-toggle btn' data-toggle='dropdown' style='border: 1px darkgrey solid'>"+val.tagName+"  <i class='fa fa-angle-down'></i></a>" +
                    "<ul class='dropdown-menu media-list pull-left animated fadeInDown'>" +
                    "<a href='javascript:;' uuid='"+val.tagUuid+"' name='"+val.tagName+"' onclick='updateTagName(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-pencil-square-o p-r-10'></i>修改名称</h5></div></div></li></a>" +
                    "<a href='javascript:;' uuid='"+val.tagUuid+"' onclick='deleteTag(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-trash-o p-r-15'></i>删除</h5></div></div></li></a>" +
                    "</ul></li>";
                $("#sale-tag-lists").append(button);
            });
        }
        var isselectALl = r.data.isselect;
        if(isselectALl == "1"){
            $("#multicheckBox").attr("checked","checked");
            $("#multiLabel").addClass("active");
        }else{
            $("#multicheckBox").removeAttrs("checked");
            $("#multiLabel").removeClass("active");
        }
        $("#multicheckBox").attr("uuid",uuid);
    });
}

/*加载添加标签界面*/
function loadCreateTag(){
    var url = ctx+"saleConfig/loadCreateTag";
    $.post(url,{},function(r){
        $("#sale-modal").empty().append(r).modal("show").draggable();
    });
}
/**删除标签组*/
function loadDelTagGroup(btn) {
  var tagUuid = $(btn).attr("uuid");
    $JuXue.confirm({
        text: "确定删除该标签组？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'saleConfig/deleteTagGroup', {"tagUuid": tagUuid}, function (r) {
            if (r.code === 0) {getAllTagGroup();$JuXue.n_success(r.msg);
            } else {$JuXue.n_danger(r.msg);}
        });
    });
}

/*加载修改分组*/
function loadUpdateGroup(btn){
    var name = $(btn).attr("name");
    $("#sale-tag-nameid").val(name);
    $.post(ctx+"saleConfig/loadUpdateGroup",{},function(r){
        $("#sale-modal").empty().append(r).modal("show").draggable();
    });
}

/*删除标签*/
function deleteTag(btn) {
    var uuid = $(btn).attr("uuid");
    $JuXue.confirm({
        text: "确定删除该标签？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'saleConfig/deleteTag', {"tagUuid": uuid}, function (r) {
            if (r.code === 0) {afterAdd();$JuXue.n_success(r.msg);
            } else {$JuXue.n_danger(r.msg);}
        });
    });
}

/*加载修改标签名称页面*/
function updateTagName(btn){
    var name = $(btn).attr("name");
    var uuid = $(btn).attr("uuid");
    $("#sale-tag-nameid").val(name);
    $("#sale-tag-upid").val(uuid);
    $.post(ctx+"saleConfig/loadUpdateTag",{},function (r) {
        $("#sale-modal").empty().append(r).modal("show").draggable();
    });
}